home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / LOGMIN_E.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  13KB  |  387 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : LOGMIN_E.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *      This is the actual minimization algorithm. Variation from the actual
  8.  *    LOGMIN algorithm is as follows:
  9.  *        1.    Minterms with adjacency 0 or 1 is minimised first
  10.  *        2.    Select next minterm with lowest adjacency wrt b-array
  11.  *        3.    To shrink the CPT, select an adjacent term, mi that has the
  12.  *            largest wi AND is already covered
  13.  *        4.  If more than one such adjacent terms are covered, select one
  14.  *        with the lowest adjacency wrt to b-array
  15.  *
  16.  * --------------------------------------------------------------------------
  17.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  18.  *    University.
  19.  *
  20.  *    You are free to use, copy and distribute this software and its
  21.  *    documentation providing that:
  22.  *
  23.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  24.  *
  25.  *        IT IS NOT MODIFIED IN ANY WAY.
  26.  *
  27.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  28.  *
  29.  *    This program is provided "AS IS" without any warranty, expressed or
  30.  *    implied, including but not limited to fitness for any particular
  31.  *    purpose.
  32.  *
  33.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  34.  *    appreciated. Please send to:
  35.  *
  36.  *        Boon-Tiong Tan or Othman Bin Ahmad
  37.  *        School of EEE
  38.  *        Nanyang Technological University
  39.  *        Nanyang Avenue
  40.  *        Singapore 2263
  41.  *        Republic of Singapore
  42.  *
  43.  ***************************************************************************/
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #define mask8 255
  49.  
  50. unsigned char   *logmin_e(a, b)              /* pointer to a & b array passed */
  51. unsigned char   *a, *b;
  52.  
  53. {
  54.    unsigned short   pterm, ma, mb, *pm, *pm1, pos;
  55.    unsigned  long   m, k, count, count1, topow();
  56.    register  long   i, wo, wi;
  57.    register  char   j;
  58.          char   test;
  59.    unsigned  char   *c, *d, *e, *s, *temp, adj0, adji;
  60.    unsigned  char   n, adj, adjacency(), nspm, cover;
  61.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  62.  
  63.  
  64.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  65.  
  66.    n    = *a;                          /* no. of variables */
  67.    nspm = *(a+3);                      /* no. of bytes/minterm */
  68.    ma = *(a+2)<<8 | *(a+1);            /* no. of minterms in a-array */
  69.  
  70.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  71.    if (temp == 0)
  72.       {
  73.      printf("Out of memory -- LOGMIN_E, *temp\n");
  74.      printf("Program terminated - 1\n");
  75.      exit(0);
  76.       }
  77.  
  78.    s = (unsigned char *) malloc(4);                /* header of s-array */
  79.    if (s == 0)
  80.       {
  81.      printf("Out of memory -- LOGMIN_E, *s\n");
  82.      printf("Program terminated - 2\n");
  83.      exit(0);
  84.       }
  85.  
  86.    pterm = 0;                                /* no. of product term */
  87.  
  88.    /***
  89.     ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  90.     ***/
  91.  
  92.    for (i=0; i<mb; i++)                      /* for adj = 0 or 1 */
  93.       {
  94.      *b = n;                             /* assign back to n */
  95.  
  96.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  97.      c = adjacent(temp, a, 1);           /* obtain the adjacent terms */
  98.      adj = *(c+1);                       /* adjacency of minterm */
  99.  
  100.      if (adj <= 1)                       /* adjacency 0 or 1 */
  101.         {
  102.            d = cpt(c);                   /* generate CPT */
  103.  
  104.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  105.            if (s == 0)
  106.           {
  107.              printf("Out of memory -- LOGMIN_E, *s\n");
  108.              printf("Program terminated - 3\n");
  109.              exit(0);
  110.           }
  111.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  112.            pterm++;                      /* product term count */
  113.  
  114.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  115.            i = (char) *b;                /* index pointer of b-array */
  116.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  117.  
  118.            free(d);                    /* free pointer */
  119.         }
  120.      free(c);                            /* free pointer */
  121.       }
  122.  
  123.     /***
  124.      ***  MINIMIZE MINTERMS WITH ADJACENCY GREATER THAN 1
  125.      ***/
  126.  
  127.     while (mb > 0)
  128.        {
  129.       adj0 = 255;                     /* max for 8-bit */
  130.  
  131.       *b = n;                         /* assign back to n */
  132.  
  133.       /***
  134.        ***  SELECT 1ST MINTERM WITH LOWEST ADJACENCY WRT B-ARRAY
  135.        ***/
  136.  
  137.       for (i=0; i<mb; i++)
  138.          {
  139.         memcpy(temp, (b+4+nspm*i), nspm);    /* pick a minterm */
  140.         adji = adjacency(temp, b);           /* obtain adjacency */
  141.  
  142.         if (adji<adj0)                       /* look for lowest adj */
  143.            {
  144.               adj0 = adji;
  145.               pos = i;                       /* position of minterm */
  146.            }
  147.           }
  148.        memcpy(temp, (b+4+nspm*pos), nspm);       /* pick the required minterm */
  149.  
  150.        /***
  151.         ***  GENERATE SSM AND TEST FOR COVERAGE BY FUNCTION
  152.         ***/
  153.  
  154.        c = adjacent(temp, a, 255);          /* obtain adjacent terms */
  155.        adj = *(c+1);                        /* adjacency of minterms */
  156.        d = cpt(c);                          /* generate CPT */
  157.        e = ssm(d);                          /* generate SSM */
  158.        m = topow(*(e+1));                   /* no. of SSM terms */
  159.  
  160.        for (j=0; j<m; j++)                  /* check for SSM coverage */
  161.           {
  162.          memcpy (temp, (e+4+nspm*j), nspm);  /* pick SSM term */
  163.          test = exist (temp, a, ma);
  164.          if (test == -1)                /* SSM term not in a-array */
  165.              break;
  166.           }
  167.  
  168.        /***
  169.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  170.         ***/
  171.  
  172.        if (test == 0)                       /* all SSM's covered by fn */
  173.           {
  174.          if (m > 65536)                 /* too many SSM terms */
  175.             {
  176.                printf("Product Term Too Huge \n");
  177.                printf("Program terminated \n");
  178.                exit(0);
  179.             }
  180.          *(e+1) = (m-1) & mask8;        /* no. of SSM terms minus 1 */
  181.          *(e+2) = (m-1)>>8 & mask8;
  182.          b = reduce(e,b,i);             /* remove minterms covered from b-array */
  183.          mb = *(b+2)<<8 | *(b+1);       /* no. of minterms in b-array */
  184.  
  185.          s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  186.          if (s == 0)
  187.             {
  188.                printf("Out of memory -- LOGMIN_E, *s\n");
  189.                printf("Program terminated - 4\n");
  190.                exit(0);
  191.             }
  192.          memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  193.          pterm++;                       /* no. of product terms */
  194.  
  195.          free(d);                       /* free pointers */
  196.          free(e);
  197.           }
  198.        else
  199.           {
  200.          /***
  201.           ***  SSM NOT COVERED BY THE FUNCTION
  202.           ***/
  203.  
  204.          free(d);                       /* free pointer */
  205.          cover =0;                      /* coverage status */
  206.  
  207.          while (!cover)        /* do until shrinked SSM is covered */
  208.             {
  209.                /***
  210.             ***  OBTAIN AN ARRAY, PM OF POSITIONS OF ADJACENT TERMS, MI WITH THE LARGEST WI
  211.             ***/
  212.  
  213.                wo = -1;                        /* initial value */
  214.                for (j=0; j<adj; j++)           /* do for all adjacent terms */
  215.               {
  216.                  memcpy(temp,(c+4+nspm*(j+1)),nspm); /* pick adjacent term */
  217.  
  218.                  wi = adj_of_mi(temp,e,a);           /* obtain wi */
  219.                  if (wi>wo)
  220.                 {
  221.                    if (wo != -1)                 /* not the first */
  222.                       free(pm);
  223.                    wo = wi;                      /* new lowest value */
  224.                    count = 1;                    /* reset count to 1 */
  225.  
  226.                    pm = (unsigned short *) malloc(2);  /* space for pm */
  227.                    if (pm==0)
  228.                       {
  229.                      printf("Out of memory -- LOGMIN_E, *pm\n");
  230.                      printf("Program terminated - 5\n");
  231.                      exit(0);
  232.                       }
  233.                    *pm = j;                      /* first element */
  234.                 }
  235.                  else if (wi==wo)                    /* wi as large */
  236.                 {
  237.                    count++;                      /* no. of element in pm-array */
  238.  
  239.                    pm = (unsigned short *) realloc (pm, 2*count);  /* more space */
  240.                    if (pm==0)
  241.                       {
  242.                      printf("Out of memory -- LOGMIN_E, *pm\n");
  243.                      printf("Program terminated - 6\n");
  244.                      exit(0);
  245.                       }
  246.                    *(pm+count-1) = j;            /* elements of pm-array */
  247.                 }
  248.               }
  249.                free(e);                                  /* free pointer */
  250.  
  251.                /***
  252.             ***  DETERMINE FROM PM-ARRAY, AN ARRAY, PM1 OF POSITION OF ADJACENT TERMS
  253.             ***  THAT HAVE ALREADY BEEN COVERED
  254.             ***/
  255.  
  256.                count1 = count;                           /* no. of elements in pm & pm1 */
  257.  
  258.                pm1 = (unsigned short *) malloc(2);       /* space for pm1 */
  259.                if (pm1==0)
  260.               {
  261.                  printf("Out of memory -- LOGMIN_E, *pm1\n");
  262.                  printf("Program terminated - 7\n");
  263.                  exit(0);
  264.               }
  265.  
  266.                k = 0;
  267.                for (j=0; j<count; j++)              /* do for all element in pm-array */
  268.               {
  269.                  memcpy(temp, (c+4+nspm*(1+ *(pm+j))), nspm);    /* pick adj term */
  270.                  test = exist(temp,b,mb);
  271.                  if (test == -1)                               /* already covered */
  272.                 {
  273.                    memcpy((pm1+k++), (pm+j), 2);       /* transfer pos to pm1 */
  274.                    count1--;                                       /* reduced */
  275.  
  276.                    pm1 = (unsigned short *) realloc(pm1, 2*(k+1)); /*  more space */
  277.                    if (pm1==0)
  278.                       {
  279.                      printf("Out of memory -- LOGMIN_E, *pm1\n");
  280.                      printf("Program terminated - 8\n");
  281.                      exit(0);
  282.                       }
  283.                 }
  284.               }
  285.  
  286.                /***
  287.             ***  SELECT FROM PM1-ARRAY, THE 1ST ADJACENT TERM, MI WITH THE LOWEST
  288.             ***  ADJACENCY WRT B-ARRAY
  289.             ***/
  290.  
  291.                adj0 = 255;                     /* max of 8-bit */
  292.                for (j=0; j<k; j++)             /* do for all in pm1-array */
  293.               {
  294.                  memcpy(temp,(c+4+nspm*(1+*(pm1+j))),nspm);  /* pick an adj term */
  295.                  adji = adjacency(temp,b);                   /* obtain adjacency */
  296.  
  297.                  if (adji<adj0)                        /* new lowest found */
  298.                 {
  299.                    adj0 = adji;                    /* update lowest */
  300.                    pos = *(pm1+j);                 /* position of lowest adj term */
  301.                 }
  302.               }
  303.  
  304.                if (count1==count)                /* select the 1st if all not covered */
  305.               pos = *pm;
  306.  
  307.                adj--;                            /* no. of adj terms in c-array */
  308.                *(c+1) = adj;                     /* adjacency after shrinking CPT */
  309.  
  310.                free(pm);                         /* free pointers */
  311.                free(pm1);
  312.  
  313.                /***
  314.             ***  SHRINK CPT (REMOVE 1 ADJACENT TERM FROM C-ARRAY), GENERATE NEW CPT, SSM
  315.             ***  AND TEST FOR COVERAGE BY FUNCTION
  316.             ***/
  317.  
  318.                memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos));  /* shrink CPT */
  319.  
  320.                c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* reduce space */
  321.                if (c==0)
  322.               {
  323.                  printf("Out of memory -- LOGMIN_E, *c\n");
  324.                  printf("Program terminated - 9\n");
  325.                  exit(0);
  326.               }
  327.  
  328.                d = cpt(c);                  /* generate CPT */
  329.                e = ssm(d);                  /* generate SSM */
  330.                m = topow(*(e+1));           /* no. of SSM terms */
  331.  
  332.                for (k=0; k<m; k++)          /* check SSM coverage by function */
  333.               {
  334.                  memcpy(temp, (e+4+nspm*k), nspm);  /* pick SSM term */
  335.                  test = exist(temp,a,ma);
  336.                  if (test == -1)                    /* SSM term not covered */
  337.                 break;
  338.               }
  339.  
  340.                /***
  341.             ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY
  342.             ***  AND SELECT SHRINKED CPT AS PRODUCT TERM
  343.             ***/
  344.  
  345.                if (test==0)                   /* SSM covered */
  346.               {
  347.                  if (m > 65536)                  /* too many SSM terms */
  348.                 {
  349.                    printf("Product Term Too Huge \n");
  350.                    printf("Program terminated \n");
  351.                    exit(0);
  352.                 }
  353.                  *(e+1) = (m-1) & mask8;         /* no. of SSM terms minus 1 */
  354.                  *(e+2) = (m-1)>>8 & mask8;
  355.                  b = reduce(e, b, i);     /* remove minterms covered from b-array */
  356.                  mb = *(b+2)<<8 | *(b+1); /* no. of minterms in b-array */
  357.  
  358.                  s = (unsigned char *) realloc(s, 4+n*(pterm+1));  /* more space */
  359.                  if (s==0)
  360.                 {
  361.                    printf("Out of memory -- LOGMIN_E, *s\n");
  362.                    printf("Program terminated - 10\n");
  363.                    exit(0);
  364.                 }
  365.                  memcpy ((s+4+n*pterm), d, n);      /* add shrinked CPT to soln array */
  366.                  pterm++;                           /* no. of product terms */
  367.  
  368.                  cover = 1;                         /* coverage status */
  369.                  free(e);                           /* free pointer */
  370.               }
  371.                free (d);                      /* free pointer */
  372.             }
  373.           }
  374.       free(c);                   /* free pointer */
  375.       }
  376.    *s = n;                           /* no. of variables */
  377.    *(s+1) = pterm & mask8;           /* no. of product terms in 2 bytes */
  378.    *(s+2) = pterm>>8 & mask8;
  379.  
  380.    free(temp);                       /* free pointer */
  381.    free(a);
  382.    free(b);
  383.  
  384.    return(s);                        /* return solution array */
  385. }
  386.  
  387.